#include "windows/addstudent.h"
#include "ui_addstudent.h"

#include <QMessageBox>

AddStudent::AddStudent(QTableWidget *_tableList,
                       StudentListType *_StudentList,
                       StudentParameterType *_StuParameters,
                       QWidget *parent) : tablewidget(_tableList),
                                          StudentList(_StudentList),
                                          StuParameters(_StuParameters),
                                          QWidget(parent),
                                          ui(new Ui::AddStudent)
{
    ui->setupUi(this);
    change_ui();

    labStudNum = new QLabel();

    // 设置窗口阻塞态
    setWindowModality(Qt::ApplicationModal);
}

AddStudent::~AddStudent()
{
    delete ui;
    delete &QLineList;
}

void AddStudent::change_ui()
{
    for (auto &s : *StuParameters)
    {
        if (s == "总分" || s == "排名" || s == "优良等级")
            break;
        QLabel *label_3 = new QLabel(ui->widget);
        label_3->setObjectName("label_3");
        QFont font2;
        font2.setPointSize(11);
        label_3->setFont(font2);
        label_3->setAlignment(Qt::AlignCenter);
        label_3->setText(QString::fromStdString(s));

        QHBoxLayout *horizontalLayout_3 = new QHBoxLayout();
        horizontalLayout_3->addWidget(label_3);

        QLineEdit *lineEdit_2 = new QLineEdit(ui->widget);
        lineEdit_2->setObjectName("lineEdit_2");
        QLineList << lineEdit_2;

        horizontalLayout_3->addWidget(lineEdit_2);

        ui->verticalLayout_2->addLayout(horizontalLayout_3);
    }
}

void AddStudent::on_pushButton_clicked()
{
    StudentsClassDef *student = new StudentsClassDef();
    int i = 0;
    int currentRow = tablewidget->rowCount();
    tablewidget->setRowCount(currentRow + 1);
    for (auto &s : *StuParameters)
    {
        if (s == "总分" || s == "排名" || s == "优良等级")
            break;
        student->set_Parameter(s, QLineList[i]->text().toStdString());
        tablewidget->setItem(currentRow, i, new QTableWidgetItem(QLineList[i]->text()));
        tablewidget->item(currentRow, i++)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    }
    StudentList->push_back(student);

    (*StudentList)[currentRow]->set_totalScore();
    (*StudentList)[currentRow]->set_rank(StudentList);
    (*StudentList)[currentRow]->set_level(StudentList->size());

    float totalScore = (*StudentList)[currentRow]->totalScore;
    float rank = (*StudentList)[currentRow]->rank;
    string level = (*StudentList)[currentRow]->level;

    tablewidget->setItem(currentRow, i, new QTableWidgetItem(QString("%1").arg(totalScore)));
    tablewidget->item(currentRow, i++)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

    tablewidget->setItem(currentRow, i, new QTableWidgetItem(QString("%1").arg(rank)));
    tablewidget->item(currentRow, i++)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

    tablewidget->setItem(currentRow, i, new QTableWidgetItem(QString::fromStdString(level)));
    tablewidget->item(currentRow, i++)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

    QMessageBox::information(nullptr, "学生信息", "添加成功");

    labStudNum->setText(QString("学生数量：%1").arg(StudentList->size()));

    hide();
}

void AddStudent::on_pushButton_2_clicked()
{
    hide();
}
